import { ActionTree } from "vuex";
import jwtCode from "jwt-decode";
import { getUserRouters } from "@/api/api";
import { formatRouter } from "@/util/public";

const actions: ActionTree<any, any> = {
  async setUser({ state, commit }, user: any) {
    // const decoded: any = jwtCode(user);
    const decode = JSON.parse(user),
      userRouters = await getUserRouters({ key: decode.name }),
      newRouters: any = [
        {
          path: "/home",
          name: "home",
          meta: { title: "首页", icon: "fa el-icon-s-home" },
          component: () => import("@/views/home/index.vue"),
        },
        ...formatRouter(userRouters.data),
      ];
    commit("SET_USER", decode);
    commit("SET_ROUTERS", newRouters);
    commit("SET_AUTH", true);

  },
};

export default actions;
